package study.sql

import org.apache.spark.SparkConf
import org.apache.spark.sql.SparkSession

/**
 * 自定义UDF函数
 *
 * @author zh
 * @date 2021/5/26 18:08
 */
object CustomUDF {
  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf().setMaster("local").setAppName("sparksql")

    val spark = SparkSession.builder().config(sparkConf).getOrCreate()
    // 隐式转换
    import spark.implicits._

    val dataFrame = spark.read.json("data/user.json")
    dataFrame.createOrReplaceTempView("user")

    // 自定义udf函数
    spark.udf.register("prefix",(name:String,prefix:String) =>{
      prefix+":"+name
    })

    spark.sql("select prefix(name,'name') from user").show()


    spark.close()
  }
}
